package com.mywang.weitravel.modules.login;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mywang.weitravel.R;
import com.mywang.weitravel.base.BaseActivity;
import com.mywang.weitravel.common.Contacts;
import com.mywang.weitravel.model.UserBean;
import com.mywang.weitravel.modules.MainActivity;
import com.mywang.weitravel.modules.mine.ResetPwdActivity;
import com.mywang.weitravel.modules.register.RegisterActivity;
import com.mywang.weitravel.util.PreferencesUtil;
import com.mywang.weitravel.util.ScreenUtil;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View, CustomAdapt {

    @BindView(R.id.et_user_name)
    EditText etUserName;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;

    @Override
    protected int getActivityLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected LoginPresenter initPresenter() {
        return new LoginPresenter(this);
    }


    @OnClick({R.id.btn_login, R.id.btn_register, R.id.tv_forget_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_forget_password:
                ResetPwdActivity.launch(LoginActivity.this);
                break;
            case R.id.btn_login:
                doLogin();
                break;
            case R.id.btn_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void doLogin() {
        String userName = etUserName.getText().toString();
        String password = etPassword.getText().toString();
        if (userName.isEmpty()) {
            ToastUtils.showShort("请输入用户名");
            return;
        }
        if (userName.length() < 6) {
            ToastUtils.showShort("请输入6-20位用户名");
            return;
        }
        if (password.isEmpty()) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (userName.length() < 6) {
            ToastUtils.showShort("请输入6-12位密码");
            return;
        }

        mPresenter.doLogin(userName, password);
    }

    @Override
    public void loginSuccess(UserBean userBean) {
        PreferencesUtil.getInstance(this).setLogin(true);
        PreferencesUtil.getInstance(this).saveParam(Contacts.SP_USER_INFO, userBean);
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean isBaseOnWidth() {
        if (ScreenUtil.isScreenSizeLarge(this)) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public float getSizeInDp() {
        if (ScreenUtil.isScreenSizeLarge(this)) {
            return 667;
        } else {
            return 360;
        }
    }
}
